﻿
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Windows;
using eLearning.Model;

namespace eLearning.Util
{
    public class Setting
    {
        private Dictionary<string, Menu> menus;
        private Menu menu1 = new Menu();
        private Menu menu2 = new Menu();
        private Menu menu3 = new Menu();
        private Menu menu4 = new Menu();
        private Menu menu5 = new Menu();

        private static IsolatedStorageSettings appSettings;

        private static readonly Setting savedSetting = new Setting();

        public static Setting Instance
        {
            get
            {
                appSettings = IsolatedStorageSettings.ApplicationSettings;
                return savedSetting;
            }
        }

        public void DestroyUser()
        {
            menus = null;
        }

        public bool SaveMenuSetting(Dictionary<string, Menu> menus)
        {
            try
            {
                User user = SavedUser.Instance.User;
                if (menus != null && user != null && (user.Type == User.UserType.Dosen || user.Type == User.UserType.Admin))
                {
                    appSettings.Remove(Config.MENU1_DOSEN);
                    appSettings.Remove(Config.MENU2_DOSEN);
                    appSettings.Remove(Config.MENU3_DOSEN);
                    appSettings.Remove(Config.MENU4_DOSEN);
                    appSettings.Remove(Config.MENU5_DOSEN);

                    appSettings.Add(Config.MENU1_DOSEN, menus["Menu1"]);
                    appSettings.Add(Config.MENU2_DOSEN, menus["Menu2"]);
                    appSettings.Add(Config.MENU3_DOSEN, menus["Menu3"]);
                    appSettings.Add(Config.MENU4_DOSEN, menus["Menu4"]);
                    appSettings.Add(Config.MENU5_DOSEN, menus["Menu5"]);
                }
                else if (menus != null && user != null && user.Type == User.UserType.Mahasiswa)
                {
                    appSettings.Remove(Config.MENU1_MHS);
                    appSettings.Remove(Config.MENU2_MHS);
                    appSettings.Remove(Config.MENU3_MHS);
                    appSettings.Remove(Config.MENU4_MHS);
                    appSettings.Remove(Config.MENU5_MHS);

                    appSettings.Add(Config.MENU1_MHS, menus["Menu1"]);
                    appSettings.Add(Config.MENU2_MHS, menus["Menu2"]);
                    appSettings.Add(Config.MENU3_MHS, menus["Menu3"]);
                    appSettings.Add(Config.MENU4_MHS, menus["Menu4"]);
                    appSettings.Add(Config.MENU5_MHS, menus["Menu5"]);
                }
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message);
                return false;
            }
            return true;
        }

        public Dictionary<string, Menu> LoadMenuSetting()
        {
            User user = SavedUser.Instance.User;
            menus = new Dictionary<string, Menu>();

            if (user != null && user.Type == User.UserType.Admin)
            {
                Dictionary<string, Menu> defaultMenu = MenuAdminList.GetDefaultMenu();

                menu1 = appSettings.Contains(Config.MENU1_ADMIN) ? (Menu)appSettings[Config.MENU1_ADMIN] : defaultMenu[Config.MENU1_ADMIN];
                menu2 = appSettings.Contains(Config.MENU2_ADMIN) ? (Menu)appSettings[Config.MENU2_ADMIN] : defaultMenu[Config.MENU2_ADMIN];
                menu3 = appSettings.Contains(Config.MENU3_ADMIN) ? (Menu)appSettings[Config.MENU3_ADMIN] : defaultMenu[Config.MENU3_ADMIN];
                menu4 = appSettings.Contains(Config.MENU4_ADMIN) ? (Menu)appSettings[Config.MENU4_ADMIN] : defaultMenu[Config.MENU4_ADMIN];
                menu5 = appSettings.Contains(Config.MENU5_ADMIN) ? (Menu)appSettings[Config.MENU5_ADMIN] : defaultMenu[Config.MENU5_ADMIN];
            }
            else if (user != null && user.Type == User.UserType.Dosen)
            {
                Dictionary<string, Menu> defaultMenu = MenuDosenList.GetDefaultMenu();

                menu1 = appSettings.Contains(Config.MENU1_DOSEN) ? (Menu)appSettings[Config.MENU1_DOSEN] : defaultMenu[Config.MENU1_DOSEN];
                menu2 = appSettings.Contains(Config.MENU2_DOSEN) ? (Menu)appSettings[Config.MENU2_DOSEN] : defaultMenu[Config.MENU2_DOSEN];
                menu3 = appSettings.Contains(Config.MENU3_DOSEN) ? (Menu)appSettings[Config.MENU3_DOSEN] : defaultMenu[Config.MENU3_DOSEN];
                menu4 = appSettings.Contains(Config.MENU4_DOSEN) ? (Menu)appSettings[Config.MENU4_DOSEN] : defaultMenu[Config.MENU4_DOSEN];
                menu5 = appSettings.Contains(Config.MENU5_DOSEN) ? (Menu)appSettings[Config.MENU5_DOSEN] : defaultMenu[Config.MENU5_DOSEN];
            }
            else if (user != null && user.Type == User.UserType.Mahasiswa)
            {
                Dictionary<string, Menu> defaultMenu = MenuMahasiswaList.GetDefaultMenu();

                menu1 = appSettings.Contains(Config.MENU1_MHS) ? (Menu)appSettings[Config.MENU1_MHS] : defaultMenu[Config.MENU1_MHS];
                menu2 = appSettings.Contains(Config.MENU2_MHS) ? (Menu)appSettings[Config.MENU2_MHS] : defaultMenu[Config.MENU2_MHS];
                menu3 = appSettings.Contains(Config.MENU3_MHS) ? (Menu)appSettings[Config.MENU3_MHS] : defaultMenu[Config.MENU3_MHS];
                menu4 = appSettings.Contains(Config.MENU4_MHS) ? (Menu)appSettings[Config.MENU4_MHS] : defaultMenu[Config.MENU4_MHS];
                menu5 = appSettings.Contains(Config.MENU5_MHS) ? (Menu)appSettings[Config.MENU5_MHS] : defaultMenu[Config.MENU5_MHS];
            }

            menus.Add("Menu1", menu1);
            menus.Add("Menu2", menu2);
            menus.Add("Menu3", menu3);
            menus.Add("Menu4", menu4);
            menus.Add("Menu5", menu5);

            return menus;
        }

        public ObservableCollection<Menu> HiddenMenu()
        {
            User user = SavedUser.Instance.User;
            ObservableCollection<Menu> hiddenMenu = new ObservableCollection<Menu>();
            Dictionary<string, Menu> savedMenu = LoadMenuSetting();

            if (user != null)
            {
                hiddenMenu = user.Type == User.UserType.Dosen ? MenuDosenList.GetMenuList() : 
                    user.Type == User.UserType.Admin ? MenuAdminList.GetMenuList() : MenuMahasiswaList.GetMenuList();

                for (int i = 0; i < savedMenu.Keys.Count; i++)
                {
                    hiddenMenu.Remove(savedMenu["Menu" + (i + 1)]);
                }

            }

            return hiddenMenu;
        }
        
        public string LoadSetting(String key)
        {
            if (appSettings.Contains(key))
                return Convert.ToString(appSettings[key]);
            else 
                return Config.ROOT_URL_KEY.Equals(key) ? Config.ROOT_URL : string.Empty;
        }

        public bool SaveSetting(string key, object value)
        {
            try
            {
                if (appSettings.Contains(key))
                    appSettings.Remove(key);

                appSettings.Add(key, value);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
    }
}
